package ru.omsu.diveintoandroid.funnycats.scenes;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.view.MotionEvent;

public class MainScene extends CCLayer {
	public static CCScene scene() {
		CCScene scene = CCScene.node();
		CCLayer layer = new MainScene();
		scene.addChild(layer);
		return scene;
	}

	public MainScene() {
		initButton();
		setIsTouchEnabled(true);
		setIsKeyEnabled(true);
	}

	private void initButton() {
		initButtonsSprites();
		initButtonsPosition();
		addButtonsAsChild();
		setActions();
	}
	
	private void setActions(){
		play_button.setAction(GameScene.scene());
		about_button.setAction(AboutScene.scene());
		raiting_button.setAction(RaitingScene.scene());
	}
	
	private void initButtonsSprites(){
		play_button = new GameButton(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/play_button.png")));
		play_button.SetPressedSprite(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/play_button_pressed.png")));
		
		about_button = new GameButton(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/about_button.png")));
		about_button.SetPressedSprite(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/about_button_pressed.png")));
		
		raiting_button = new GameButton(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/raiting_button.png")));
		raiting_button.SetPressedSprite(CCSprite.sprite(CCTextureCache
				.sharedTextureCache().addImage("buttons/raiting_button_pressed.png")));
	}
	
	private void initButtonsPosition(){
		CGSize display_size = CCDirector.sharedDirector().winSize();

		play_button.SetScale(0.5f);
		play_button.SetPosition(CGPoint.ccp(display_size.width / 2, display_size.height
				- play_button.GetSprite().getTexture().getHeight() / 2));
		
		about_button.SetScale(0.5f);
		about_button.SetPosition(CGPoint.ccp(display_size.width / 2, display_size.height
				- play_button.GetSprite().getTexture().getHeight()
				- about_button.GetSprite().getTexture().getHeight() / 2));

		raiting_button.SetScale(0.5f);
		raiting_button.SetPosition(CGPoint.ccp(display_size.width / 2, display_size.height
				- play_button.GetSprite().getTexture().getHeight()
				- about_button.GetSprite().getTexture().getHeight()
				- raiting_button.GetSprite().getTexture().getHeight() / 2));
	}
	
	private void addButtonsAsChild(){
		addChild(play_button.GetSprite());
		addChild(about_button.GetSprite());
		addChild(raiting_button.GetSprite());
	}
	
	@Override
	public boolean ccTouchesBegan(MotionEvent event) {
		CGPoint convertedLocation = CCDirector.sharedDirector().convertToGL(
				CGPoint.make(event.getX(), event.getY()));

		if(play_button.isPointOnButton(convertedLocation)){
			play_button.ClickOnButton(this);
			play_button.doAction();
		}

		if(about_button.isPointOnButton(convertedLocation)){
			about_button.ClickOnButton(this);
			about_button.doAction();
		}
		
		if(raiting_button.isPointOnButton(convertedLocation)){
			raiting_button.ClickOnButton(this);
			raiting_button.doAction();
		}
		
		return super.ccTouchesBegan(event);
	}
	
	@Override
	public boolean ccTouchesEnded(MotionEvent event) {
		play_button.setDefaultState(this);
		about_button.setDefaultState(this);
		raiting_button.setDefaultState(this);
		
		return super.ccTouchesEnded(event);
	}

	private GameButton play_button;
	private GameButton about_button;
	private GameButton raiting_button;
}
